package com.chefeiyue.biz.controller;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.support.RequestContextUtils;

import com.chefeiyue.biz.service.UserService;
import com.chefeiyue.biz.service.flow.AQICEvent;
import com.chefeiyue.biz.service.flow.FlowEngineProxy;
import com.chefeiyue.db.model.User;

@Controller
@RequestMapping(value="/user")
public class UserController {
	
	@Autowired
	private UserService userServiceImpl;
	
	@RequestMapping(value="/add", method=RequestMethod.POST)
	public String addUser(HttpSession httpSession,@ModelAttribute("user") User user) {
		
		userServiceImpl.addUser(user);
		System.out.println("12111111134");
		//WebApplicationContext
		System.out.println("2222222222");
		ApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(httpSession.getServletContext()); 
		FlowEngineProxy obj=(FlowEngineProxy)ctx.getBean("MyFlowEngineProxy");
		System.out.println(obj.toString());
		AQICEvent e=new AQICEvent();
		obj.proEvent(e);
		System.out.println(123);
		System.out.println(456);
		
		return "redirect:list/" + user.getId();
	}
	
	@RequestMapping(value="/list/{id}", method=RequestMethod.GET)
	public ModelAndView listUser(@PathVariable Integer id) {
		
		User user = userServiceImpl.getUserById(id);
		ModelAndView model = new ModelAndView();
		model.setViewName("result");
		model.addObject("user", user);
		return model;
	}
 	
}
